"""
|===== Conference Bot Plugin ============
|= Moderator Commands
|===== By: ==============================
|= Patrick Kennedy
|===== Current Version: =================
|= 1.0
|===== Description: =========================================
|= Admins and Mods can use these commands.
|===== Additional Comments: =================================
|= 
|============================================================"""

from utils import *

def cmd_invite(bot, user, args):
	"""Invite a user to the room.
	Usage: )invite <username>"""
	jid = getjid(args.lower())
	if jid:
		bot.addUser(jid)
		adduser(jid)
		bot.msg(user, "%s has been invited." % jid)
	
	else:
		bot.msg(user,cmd_invite.__doc__)
	
def cmd_kick(bot, user, args):
	"""Removes a user from the room.
	Usage: )kick <username>"""
	jid = getjid(args.lower())
	if jid:
		bot.removeUser(jid)
		bot.rejectUser(jid)
		del userlist[jid]
		bot.msg(user, "%s has been kicked." % jid)
		
	else:
		bot.msg(user, cmd_kick.__doc__)

def cmd_ban(bot, user, args):
	"""Ban and kick a user from the room.
	Usage: )ban <username>"""
	jid = getjid(args.lower())
	if jid:
		addban(jid)
		bot.removeUser(jid)
		bot.rejectUser(jid)
		bot.msg(user, "%s has been banned." % jid)
		
	else:
		bot.msg(user,cmd_ban.__doc__)
		
def cmd_mute(bot, who, msg):
	"""Mute/Unmute a user.
	Usage: )mute <username>
	Note: To unmute a user simply use the command again."""
	jid = getjid(msg.strip().lower())
	if msg:
		if delmute(jid):
			bot.systoall('<%s> has been unmuted' % getdisplayname(jid))
			bot.sys(jid, 'You have been unmuted by <%s>' % getdisplayname(who))
		elif not isadmin(jid) and not who == jid:
			addmute(jid)
			if ' ' in msg:
				mutee, msg = msg.split(' ',1)
				bot.sys(jid, '<%s> has muted you for %s.' % getdisplayname(who), msg)
			else:
				bot.sys(jid, '<%s> has muted you.' % getdisplayname(who))
			bot.systoall('<%s> has been muted.' % getdisplayname(jid))
		saveuserlist()
	else:
		bot.msg(user, cmd_mute.__doc__)

def hook_mute(bot, args):
	user = getjid(args[0])
	if ismuted(user):
		bot.sys(user, "You are muted and therefore can't send any messages.\nPlease type ')help' for a list of usable commands.")
		return True
	return False
		
def cmd_status(bot, user, msg):
	"""Change the bot's status.
	Usage: )status [<command>:]<message>
	
	Example:
	 ')status busy:Down for maintanence'
	
	Commands:
	 reload - Use stored status.
	 offline
	 away
	 busy
	 idle
	
	Note:
	 Any commands with nothing after the ':'
	will have blank messages. 
	"""
	if msg:
		status = None
		
		if msg.lower() == "reload" or msg[:msg.find(":")].lower() == "reload":
			bot.setOnline(module.system.status)
			
		elif msg[:msg.find(":")].lower() in ["busy","dnd"]:
			status = msg[msg.find(":") + 1:]
			bot.setDND(status)
			if status:
				bot.msg(user, "The bot is now busy with the status:\n%s" % status)
			else:
				bot.msg(user, "The bot is now busy.")
				
		elif msg[:msg.find(":")].lower() == "away":
			status = msg[msg.find(":") + 1:]
			bot.setAway(status)
			if status:
				bot.msg(user, "The bot is now away with the status:\n%s" % status)
			else:
				bot.msg(user, "The bot is now away.")
				
		elif msg[:msg.find(":")].lower() in ["idle","xa"]:
			status = msg[msg.find(":") + 1:]
			bot.setXA(status)
			if status:
				bot.msg(user, "The bot is now idle with the status:\n%s" % status)
			else:
				bot.msg(user, "The bot is now idle.")
				
		elif msg.lower() == "offline" or msg[:msg.find(":")].lower() == "offline":
			bot.Error(user, "This feature not yet implemented.")
			#bot.setUnavailable()
			#bot.msg(user, "The bot is now invisable.")
			
		else:
			module.system.status = msg
			bot.setOnline(msg)
			bot.msg(user, "The bot's status is now: %s" % msg)
			module.save()
	else:
		bot.msg(user, cmd_status.__doc__)
		
def init(bot):
	bot.addModCommand("invite", cmd_invite)
	bot.addModCommand("kick", cmd_kick)
	bot.addModCommand("ban", cmd_ban)
	bot.addModCommand("mute", cmd_mute)
	bot.addHook("message", hook_mute)
	bot.addModCommand("status", cmd_status)
	bot.addModCommand("topic", cmd_status)
